package pigshare.picture;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

import pigshare.beans.LoginBean;
import pigshare.database.DatabaseManager;

@ManagedBean(name = "huhuBean")
@SessionScoped
public class HuhuBean {

	public DefaultStreamedContent content;
	private Blob bl;
	private InputStream stream;

	private Logger log = Logger.getLogger(LoginBean.class);

	public HuhuBean() {

	}

	public StreamedContent getContent(String idus) {
		ExternalContext externalContext = FacesContext.getCurrentInstance()
				.getExternalContext();
		String b = externalContext.getRequestParameterMap().get("b");
		String subId = externalContext.getRequestParameterMap().get("action");
		log.info("foto id: subID " + subId + " b " + b + " idus " + idus);
		if (idus == null) {
			ResultSet rs = DatabaseManager.getDatabaseManager().sendQuery(
					"SELECT  `blob` FROM posts WHERE  `id` = '" + b + "';");
			try {
				if (rs.next()) {
					bl = rs.getBlob("blob");
					log.info("length of the picture " + b + " " + bl.length());
					stream = bl.getBinaryStream();
					BufferedInputStream in = new BufferedInputStream(stream);
					ByteArrayOutputStream out = new ByteArrayOutputStream();

					int val = -1;
					try {
						while ((val = in.read()) != -1)
							out.write(val);
					} catch (IOException e) {
						e.printStackTrace();
					}

					byte[] bytes = out.toByteArray();
					content = new DefaultStreamedContent(
							new ByteArrayInputStream(bytes), "image/png",
							"testw.png");
				}
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			return content;
		} else {
			ResultSet rs = DatabaseManager.getDatabaseManager().sendQuery(
					"SELECT  `blob` FROM posts WHERE  `id` = '" + idus + "';");
			try {
				if (rs.next()) {
					bl = rs.getBlob("blob");
					log.info("length of the picture " + b + " " + bl.length());
					stream = bl.getBinaryStream();
					BufferedInputStream in = new BufferedInputStream(stream);
					ByteArrayOutputStream out = new ByteArrayOutputStream();

					int val = -1;
					try {
						while ((val = in.read()) != -1)
							out.write(val);
					} catch (IOException e) {
						e.printStackTrace();
					}

					byte[] bytes = out.toByteArray();
					content = new DefaultStreamedContent(
							new ByteArrayInputStream(bytes), "image/png",
							"testw.png");
				}
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			return content;
		}
	}

	public void setContent(DefaultStreamedContent content) {
		this.content = content;
	}

}
